import { Breadcrumb, Button, message } from 'antd';
import React, { FC } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { PageContainer } from '@/component/page-container';
import styles from './index.less';
import Basic from '@/view/teacher/component/basic';
import { TeachersSrv } from '@/service/teacher';
import { getPathRoot } from '@/utils';
import { getWorkFlowStatus } from '@/service';
import Prompt from '@/view/component/prompt';

const Groups: FC = () => {
  const navigate = useNavigate();
  const location = useLocation();
  const { id } = useParams();
  const teacher_id = id; //Number(id);
  const [form, setForm] = React.useState({ basicTop: {}, basicBody: {} });
  const [isAudit, setIsAudit] = React.useState<boolean>(false);

  const init = async () => {
    const resD = await TeachersSrv.detail(teacher_id);
    if (resD.success) {
      setForm(prevForm => ({
        ...prevForm,
        basicTop: { ...resD.data },
      }));
    }
    const resI = await TeachersSrv.info(teacher_id);
    if (resI.success) {
      if (resI.data.current_post_level) {
        resI.data.current_post_level = resI.data.current_post_level.split(',');
      }
      if (resI.data.health_condition) {
        resI.data.health_condition = resI.data.health_condition.split(',');
      }
      if (resI.data.highest_education) {
        resI.data.highest_education = resI.data.highest_education.split(',');
      }
      if (resI.data.current_post_type) {
        resI.data.current_post_type = resI.data.current_post_type.split(',');
      }
      if (resI.data.source_of_staff) {
        resI.data.source_of_staff = resI.data.source_of_staff.split(',');
      }
      //console.log('🚀 ~ init ~ resI.data:', resI.data);

      setForm(prevForm => ({
        ...prevForm,
        basicBody: { ...resI.data },
      }));
    }

    const res = await getWorkFlowStatus(location.state.process_instance_id);
    if (res.success) {
      setIsAudit(['pending', 'progressing'].includes(res.data));
    }

    //console.log('form', form);
  };
  React.useEffect(() => {
    // if (!location.state) {
    //   navigate(`${getPathRoot()}/employed/approve`);
    //   return;
    // }
    init();
  }, []);
  return (
    <PageContainer
      header={
        <Breadcrumb
          items={[
            {
              title: '教职工信息变更审批',
              onClick: () => {
                navigate(`${getPathRoot()}/employed/approve`);
              },
            },
            { title: '教职工信息变更审批管理' },
          ]}
        />
      }
    >
      <div className={styles.base_info}>
        <Basic
          isEdit={false}
          form={form}
          buttonList={[
            <Button onClick={() => navigate(`${getPathRoot()}/employed/approve`)}>返回</Button>,
            isAudit ? (
              <Prompt
                title="审核拒绝"
                inputLabel="审核意见"
                onOk={async (cnt: string) => {
                  const res = await TeachersSrv.rejected(teacher_id, location.state.process_instance_id, cnt);
                  if (res.success) {
                    message.success('操作成功');
                    init();
                    return true;
                  }
                  message.error(res.message || res.details);
                  return false;
                }}
              >
                拒绝
              </Prompt>
            ) : (
              ''
            ),
            isAudit ? (
              <Prompt
                title="审核通过"
                inputLabel="审核意见"
                type="primary"
                onOk={async (cnt: string) => {
                  const res = await TeachersSrv.approved(teacher_id, location.state.process_instance_id, cnt);
                  if (res.success) {
                    message.success('操作成功');
                    init();
                    return true;
                  }
                  message.error(res.message || res.details);
                  return false;
                }}
              >
                通过
              </Prompt>
            ) : (
              ''
            ),
          ]}
        />
      </div>
    </PageContainer>
  );
};

export default Groups;
